layui.use(['table', 'ax','admin','form','excel','selectM'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var excel = layui.excel;
    var selectM = layui.selectM;
    /**
     * 基础字典管理
     */
    var Rank = {
        tableId: "rankTable",
        condition:{
            rankType:2
        }
    };
    //先隐藏市选择
    $(".city").hide();

    var userLevel = $("#userLevel").val();

    //查询市
    var pAjax = new $ax(Feng.ctxPath + "/region/selectChildren", function (data) {
        $("#city").empty();
        $("#city").append('<option value="">请选择市</option>')
        $("#city").append(data)
    })
    pAjax.set("regionLevel",2)
    pAjax.set("parentId",16)
    pAjax.start();
    form.render('select');
    // var qAjax = new $ax(Feng.ctxPath + "/chart/quarterList", function (data) {
    //     $("#quarter").empty();
    //     $("#quarter").append(data)
    // })
    // qAjax.start();

    //月份多选
    var monthSelect = selectM({
        //元素容器【必填】
        elem: '#month'
        //候选数据【必填】
        ,data: [{"value":1,"name":"1月"},{"value":2,"name":"2月"},{"value":3,"name":"3月"},{"value":4,"name":"4月"},{"value":5,"name":"5月"},{"value":6,"name":"6月"},{"value":7,"name":"7月"},{"value":8,"name":"8月"},{"value":9,"name":"9月"},{"value":10,"name":"10月"},{"value":11,"name":"11月"},{"value":12,"name":"12月"},]
        ,max:12
        ,width:300
        //候选项数据的键名
        ,field: {idName:'value',titleName:'name'}
        //添加验证
        // ,verify:'required'
        ,tips:"请选择月份,默认为全年"
    });


    /**
     * 初始化表格的列
     */
    Rank.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'rank', sort: false, title: '名次'},
            // {field: 'parentName', sort: false, title: '上级单位'},
            {field: 'region_name', sort: false, title: '单位'},
            {field: 'total_score', sort: false, title: '总分'},
            {field: 'base_score', sort: false, title: '基础分'},
            {field: 'data_score', sort: false, title: '评判分'},
            {field: 'county_score',hide:userLevel!=1 || Rank.condition.rankType==3, sort: false, title: '县市区平均分'},

        ]];
    };



    /**
     * 渲染表格
     */
        // 渲染表格
    var tableResult = table.render({
            elem: '#' + Rank.tableId,
            url: Feng.ctxPath + '/chart/rankList',
            page: true,
            toolbar: true,
            height: "full-98",
            cellMinWidth: 100,
            cols: Rank.initColumn()
        });

    //搜索
    $('#btnSearch').click(function () {
        Rank.search()
    })

    //查询
    Rank.search = function () {
        var queryData = {};
        queryData['year'] = $("#year").val();
        queryData['month'] = monthSelect.values.toString();
        queryData['rankType'] = Rank.condition.rankType;
        queryData['city'] = $("#city").val();
        table.reload(Rank.tableId, {where: queryData});
    }


    form.on('radio(rankType)',function (data) {
        Rank.condition.rankType = data.value;
        if(data.value==2){
            $(".city").hide();
        }
        if(data.value==3){
            $(".city").show();
        }
        //重载表格
        tableResult = table.render({
            elem: '#' + Rank.tableId,
            url: Feng.ctxPath + '/chart/rankList',
            page: true,
            toolbar: true,
            height: "full-98",
            cellMinWidth: 100,
            cols: Rank.initColumn()
        });
        Rank.search();
    })


    //导出Excel表格
    $('#exportExcel').on('click', function(){
        var ajax = new $ax(Feng.ctxPath + "/chart/exportRankExcel", function (data) {
            // 重点！！！如果后端给的数据顺序和映射关系不对，请执行梳理函数后导出
            var data;
            if(userLevel==1){
                data = excel.filterExportData(data, [
                    'parentName'
                    ,'region_name'
                    ,'base_score'
                    ,'data_score'
                    ,'county_score'
                    ,'total_score'
                    ,'rank'
                ]);

                data.unshift({ parentName: "上级单位", region_name: "单位名称",base_score: "基础分",data_score: '评判分',county_score: '地市得分', total_score: "总得分",rank: "排名"});
            }else{
                data = excel.filterExportData(data, [
                    'parentName'
                    ,'region_name'
                    ,'base_score'
                    ,'data_score'
                    ,'total_score'
                    ,'rank'
                ]);
                // 重点2！！！一般都需要加一个表头，表头的键名顺序需要与最终导出的数据一致
                data.unshift({ parentName: "上级单位", region_name: "单位名称",base_score: "基础分",data_score: '评判分', total_score: "总得分",rank: "排名"});
            }

            // 2. 调用设置样式的函数，传入设置的范围，支持回调
            excel.setExportCellStyle(data, 'A1:L1', {
                s: {
                    font: { bold:true, fgColor: { rgb: "FF0000" } },
                    alignment: {
                        horizontal: 'center',
                        vertical: 'center'
                    }
                }
            }, function(cell, newCell, row, config, currentRow, currentCol, fieldKey) {
                // 回调参数，cell:原有数据，newCell:根据批量设置规则自动生成的样式，row:所在行数据，config:传入的配置,currentRow:当前行索引,currentCol:当前列索引，fieldKey:当前字段索引
                return (currentRow === 0) ? newCell : cell;// 第一行表头加粗
            } );

            //设置表格宽度
            var colConf = excel.makeColConfig({
                'A': 120,
                'B': 80,
                'C': 100,
                'D': 150,
                'E': 120,
            }, 80);
            //var date = new Date();
            excel.exportExcel(data,'排名列表.xlsx', 'xlsx', {
                extend: {
                    '!cols': colConf
                }
            });
        }, function (data) {
            Feng.error("导出Excel失败！" + data.responseJSON.message + "!");
        });
        ajax.set("year",$("#year").val());
        ajax.set("rankType",Rank.condition.rankType);
        ajax.set("city", $("#city").val());
        ajax.start();
    });

});
